(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.popup.adHocController', reportAdHocController);
	reportAdHocController.$inject = [ '$scope', '$q', '$uibModalInstance', 'Constants', 'dialogsModule', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig', 'authService','reportService'];
	function reportAdHocController($scope, $q, $uibModalInstance, Constants, dialogsModule, gridService, modalService, layoutConfig, validatorConfig, authService,reportService) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("reportAdHocLookup");
		ctrl.dialogsModule = dialogsModule;
		ctrl.layoutConfig = layoutConfig;
		if(!ctrl.dialogsModule.params.readOnly){
			ctrl.validatorConfig = validatorConfig;
		}
		ctrl.formName = 'reportAdHocLookupForm';
		
		ctrl.ok = function() {
			if($scope.reportAdHocLookupForm) {
				$scope.reportAdHocLookupForm.$setSubmitted();
				if(!$scope.reportAdHocLookupForm.$valid) {
					dialogsModule.showErrorMessages($scope.reportAdHocLookupForm);
					return;
				}
			}
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['ok'] || dialogsModule.btnCallback['ok'](ctrl.reportAd) !== false) {
				$uibModalInstance.close();
			}
		};
		
		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['close'] || dialogsModule.btnCallback['close']() !== false) {
				$uibModalInstance.dismiss();
			}
		};
		
		ctrl.init = function() {
			if(ctrl.dialogsModule.params.readOnly){
				var gridConfigs = ctrl.layoutConfig.formConfigs.general.gridConfigs;
				 if(gridConfigs){
					  angular.forEach(gridConfigs, function(gridConfig, key) {
						ctrl.layoutConfig.formConfigs.general.gridConfigs[key].readonly = true;
					  });
				  }
			}
			ctrl.reportAd = {};
			if (ctrl.dialogsModule.params.reportAd) {
				ctrl.reportAd = ctrl.dialogsModule.params.reportAd;// get params from parent page
			} else {
				ctrl.reportAd = {};
			}
		};
		
//		ctrl.sortByOptions = {
//			"id" : "cda.reportAdSortField",
//			"params" : {
//				"filterSortBy" : "<sortBy",
//				"filterAdrptCode" : "<adrptCode"
//			},
//			"okEvent" : true,
//			"cancelEvent" : false
//		};
		
		
		
		function selectSort(objArr) { 
			var temp;
			for (var i = 0; i < objArr.length; i++) {
				var k = i;
				for (var j = i + 1; j < objArr.length; j++) {
					if((objArr[j].sortSequence - 0) < (objArr[k].sortSequence - 0))
						k = j;
				}
				if (k != i) {
					temp = objArr[i];
					objArr[i] = objArr[k];
					objArr[k] = temp;
				}
			}
			return objArr;
		}
		
		$scope.$on("SORTBY_OK", function(event, param) {
			var returnEntities = param.returnEntities;
			if (!returnEntities || returnEntities.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return false;
			}
			returnEntities = selectSort(returnEntities);
			var backString = "";
			for(var i = 0; i < returnEntities.length; i++){
				var fieldObj = returnEntities[i];
				if(fieldObj.sortDirection == "A"){
					if(i == 0)
						backString = backString + fieldObj.fieldName; 
					else
						backString = backString + "," + fieldObj.fieldName; 
				}else if(fieldObj.sortDirection == "D"){
					if(i == 0)
						backString = backString + fieldObj.fieldName + "  DESC";
					else
						backString = backString + "," + fieldObj.fieldName + "  DESC";	
				}
			}
			ctrl.reportAd.sortBy = backString;
			//
			event.preventDefault();
			event.stopPropagation();
		});
		
//		ctrl.statisticGroupByOptions = {
//			"id" : "cda.reportAdField",
//			"params" : {
//				"filterStatisticGroupBy" : "<statisticGroupBy",
//				"filterAdrptCode" : "<adrptCode"
//			},
//			"okEvent" : true,
//			"cancelEvent" : false
//		};
		
		$scope.$on("STATISTICGROUPBY_OK", function(event, param) {
			var returnEntities = param.returnEntities;
			if (!returnEntities || returnEntities.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return false;
			}
			returnEntities = selectSort(returnEntities);
			var backString = "";
			for(var i = 0; i < returnEntities.length; i++){
				if(i == 0){
					backString = backString + returnEntities[i].fieldName;
				}else{
					backString = backString + "," + returnEntities[i].fieldName;
				}
			}
			ctrl.reportAd.statisticGroupBy = backString;
			//
			event.preventDefault();
			event.stopPropagation();
		});

	}
})(angular);
